package handler

import (
	"github.com/gin-gonic/gin"
	"tong/pkg/jwt"
	"tong/repository"
	"tong/request"
	"tong/response"
)

func Login(c *gin.Context) {
	var data request.LoginUser
	err := c.ShouldBind(&data)

	if err != nil {
		response.ResponseError(c, "错误", err.Error())
		return
	}
	user, err := repository.LoginUser(data.Name, data.Password)
	if user.Id == 0 {
		response.ResponseError(c, "用户不存在", err)
		return
	}
	claims := jwt.CustomClaims{ID: user.Id}
	token, err := jwt.NewJWT("2210A").CreateToken(claims)
	if err != nil {
		response.ResponseError(c, "token生成失败", err)
		return
	}
	response.ResponseSuccess(c, "登陆成功", token)
}
